package com.gisocn.onemap.ui.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.gisocn.onemap.base.BaseFragment;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.interfaces.OnItemClickListener;
import com.gisocn.onemap.interfaces.OnItemDeleteListener;
import com.gisocn.onemap.interfaces.OnItemLongClickListener;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.FileCenter;
import com.gisocn.onemap.model.ImageWall;
import com.gisocn.onemap.presenter.FilePackagePresenter;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.ui.activity.FileBrowseActivity;
import com.gisocn.onemap.ui.activity.MainActivity;
import com.gisocn.onemap.ui.adapter.FilePackageAdapter;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.view.FilePackageView;
import com.google.gson.Gson;
import com.tencent.smtt.sdk.TbsVideoCacheTask;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;

/* loaded from: classes2.dex */
public class FilePackageFragment extends BaseFragment<FilePackagePresenter> implements FilePackageView {
    private AppConfig appConfig;
    @BindView(R.id.clear_et_query)
    ImageView etclear;
    @BindView(R.id.et_query)
    EditText etquery;
    private Dialog fileDialog;
    FilePackageAdapter filePackageAdapter;
    @BindView(R.id.iv_menu)
    ImageView ivMenu;
    @BindView(R.id.key_search)
    ImageView keysearch;
    private String keywords;
    private View menuPopupView;
    private PopupWindow menuPopupWindow;
    FilePackageAdapter onLineAdapter;
    @BindView(R.id.recycle_file)
    RecyclerView recycleFile;
    @BindView(R.id.recycle_online_file)
    RecyclerView recycleOnlineFile;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_close)
    TextView tvClose;
    @BindView(R.id.tv_localData)
    TextView tvLocalData;
    @BindView(R.id.tv_onlineData)
    TextView tvOnlineData;
    Unbinder unbinder;
    View view;
    private ArrayList<FileCenter> fileCenterList = new ArrayList<>();
    private ArrayList<FileCenter> localFileList = new ArrayList<>();
    private ArrayList<FileCenter> onlineFileList = new ArrayList<>();
    private ArrayList<FileCenter> tempFileList = new ArrayList<>();
    private String indexPath = "";
    private String parentPath = "";
    Handler handler = new Handler() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.9
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what != 1) {
                return;
            }
            FilePackageFragment.this.onLineAdapter.notifyDataSetChanged();
        }
    };

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void onInvisible() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_file_package, viewGroup, false);
        this.view = inflate;
        this.unbinder = ButterKnife.bind(this, inflate);
        return this.view;
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void initData() {
        this.presenter = createPresenter();
        this.indexPath = Constant.SAVE_FILE_PACKAGE;
        this.appConfig = ((MainActivity) getActivity()).getAppConfig();
        initView();
        if (this.appConfig.getMediaData() != null) {
            ((FilePackagePresenter) this.presenter).getFileCenter(this.appConfig.getMediaData().getUrl().trim());
        }
        this.fileCenterList.clear();
        getFilesAllName(Constant.SAVE_FILE_PACKAGE);
        getAllLocalFile(Constant.SAVE_FILE_PACKAGE);
    }

    void initView() {
        int dimensionPixelOffset = getResources().getDimensionPixelOffset(R.dimen.dp_165);
        Log.e("item", "" + dimensionPixelOffset);
        int floor = (int) Math.floor((double) (getResources().getDisplayMetrics().widthPixels / dimensionPixelOffset));
        this.recycleFile.setLayoutManager(new GridLayoutManager(getActivity(), floor));
        FilePackageAdapter filePackageAdapter = new FilePackageAdapter(this.fileCenterList, getActivity(), 0);
        this.filePackageAdapter = filePackageAdapter;
        this.recycleFile.setAdapter(filePackageAdapter);
        this.filePackageAdapter.setOnItemClickListener(new OnItemClickListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.1
            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, int i, int i2) {
            }

            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, int i) {
                FilePackageFragment filePackageFragment = FilePackageFragment.this;
                filePackageFragment.indexPath = ((FileCenter) filePackageFragment.fileCenterList.get(i)).getHerf();
                if (((FileCenter) FilePackageFragment.this.fileCenterList.get(i)).getType() == 1) {
                    FilePackageFragment filePackageFragment2 = FilePackageFragment.this;
                    filePackageFragment2.getFilesAllName(((FileCenter) filePackageFragment2.fileCenterList.get(i)).getHerf());
                    return;
                }
                Intent intent = new Intent(FilePackageFragment.this.getActivity(), FileBrowseActivity.class);
                intent.putExtra(TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL, "local");
                Log.e("fileName", "" + ((FileCenter) FilePackageFragment.this.fileCenterList.get(i)).getName());
                intent.putExtra("fileUrl", ((FileCenter) FilePackageFragment.this.fileCenterList.get(i)).getHerf());
                intent.putExtra("fileName", ((FileCenter) FilePackageFragment.this.fileCenterList.get(i)).getName());
                ArrayList arrayList = new ArrayList();
                Iterator it = FilePackageFragment.this.fileCenterList.iterator();
                while (it.hasNext()) {
                    FileCenter fileCenter = (FileCenter) it.next();
                    String name = fileCenter.getName();
                    if (name.contains("png") || name.contains("PNG") || name.contains("jpg") || name.contains("JPG") || name.contains("gif") || name.contains("GIF") || name.contains("jpeg") || name.contains("JPEG")) {
                        arrayList.add(new ImageWall(fileCenter.getName(), fileCenter.getHerf(), i));
                    }
                }
                intent.putExtra("imageWalls", arrayList);
                FilePackageFragment.this.startActivity(intent);
            }
        });
        this.filePackageAdapter.setOnItemLongClickListener(new OnItemLongClickListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.2
            @Override // com.gisocn.onemap.interfaces.OnItemLongClickListener
            public void onItemClick(View view, int i) {
                ((FileCenter) FilePackageFragment.this.fileCenterList.get(i)).setDelete(true);
                FilePackageFragment.this.filePackageAdapter.notifyDataSetChanged();
                Log.e("setOnItemLongClick", new Gson().toJson(FilePackageFragment.this.fileCenterList.get(i)));
            }
        });
        this.filePackageAdapter.setOnItemDeleteListener(new OnItemDeleteListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.3
            @Override // com.gisocn.onemap.interfaces.OnItemDeleteListener
            public void onItemDelete(View view, int i) {
                File file = new File(((FileCenter) FilePackageFragment.this.fileCenterList.get(i)).getHerf());
                if (file.isFile()) {
                    Log.e("file.isFile()", "onItemDelete");
                    file.delete();
                }
                FilePackageFragment.deleteDirWihtFile(file);
                FilePackageFragment.this.fileCenterList.remove(i);
                FilePackageFragment.this.filePackageAdapter.notifyDataSetChanged();
            }
        });
        this.etquery.addTextChangedListener(new TextWatcher() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.4
            @Override // android.text.TextWatcher
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override // android.text.TextWatcher
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override // android.text.TextWatcher
            public void afterTextChanged(Editable editable) {
                if (editable.toString().isEmpty()) {
                    FilePackageFragment filePackageFragment = FilePackageFragment.this;
                    filePackageFragment.getFilesAllName(filePackageFragment.indexPath);
                    return;
                }
                FilePackageFragment.this.etclear.setVisibility(0);
                FilePackageFragment.this.fileCenterList.clear();
                FilePackageFragment.this.keywords = editable.toString();
                FilePackageFragment.this.test();
            }
        });
        this.recycleOnlineFile.setLayoutManager(new GridLayoutManager(getActivity(), floor));
        FilePackageAdapter filePackageAdapter2 = new FilePackageAdapter(this.onlineFileList, getActivity(), 1);
        this.onLineAdapter = filePackageAdapter2;
        this.recycleOnlineFile.setAdapter(filePackageAdapter2);
        this.onLineAdapter.setOnItemClickListener(new OnItemClickListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.5
            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, int i, int i2) {
            }

            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, int i) {
                Log.e("链接", "" + ((FileCenter) FilePackageFragment.this.onlineFileList.get(i)).getHerf());
                String str = MainActivity.MEDIADATA_PATH + ((FileCenter) FilePackageFragment.this.onlineFileList.get(i)).getHerf();
                if (((FileCenter) FilePackageFragment.this.onlineFileList.get(i)).getType() == 1) {
                    Log.e("文件夹链接", "" + ((FileCenter) FilePackageFragment.this.onlineFileList.get(i)).getHerf());
                    ((FilePackagePresenter) FilePackageFragment.this.presenter).getFileCenter(str);
                    return;
                }
                String substring = str.substring(str.lastIndexOf("/") + 1);
                if (FilePackageFragment.this.isNull(substring)) {
                    FilePackageFragment.this.showToast("无法获取文件");
                    return;
                }
                Intent intent = new Intent(FilePackageFragment.this.getActivity(), FileBrowseActivity.class);
                intent.putExtra(TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL, str);
                intent.putExtra("fileUrl", ((FileCenter) FilePackageFragment.this.onlineFileList.get(i)).getHerf());
                intent.putExtra("fileName", substring);
                ArrayList arrayList = new ArrayList();
                Iterator it = FilePackageFragment.this.onlineFileList.iterator();
                while (it.hasNext()) {
                    FileCenter fileCenter = (FileCenter) it.next();
                    String name = fileCenter.getName();
                    if (name.contains("png") || name.contains("PNG") || name.contains("jpg") || name.contains("JPG") || name.contains("gif") || name.contains("GIF") || name.contains("jpeg") || name.contains("JPEG")) {
                        try {
                            arrayList.add(new ImageWall(fileCenter.getName(), URLDecoder.decode(fileCenter.getHerf(), "utf-8"), i));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
                intent.putExtra("imageWalls", arrayList);
                FilePackageFragment.this.startActivity(intent);
            }
        });
    }

    void test() {
        Iterator<FileCenter> it = this.localFileList.iterator();
        while (it.hasNext()) {
            FileCenter next = it.next();
            if (next.getName().contains(this.keywords)) {
                this.fileCenterList.add(next);
            }
        }
        this.filePackageAdapter.notifyDataSetChanged();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseFragment
    public FilePackagePresenter createPresenter() {
        return new FilePackagePresenter(this);
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public void onDestroyView() {
        super.onDestroyView();
        this.unbinder.unbind();
    }

    @OnClick({R.id.tv_close, R.id.tv_localData, R.id.tv_onlineData, R.id.iv_menu, R.id.clear_et_query, R.id.key_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear_et_query /* 2131230840 */:
                this.etquery.setText("");
                this.fileCenterList.clear();
                this.fileCenterList.addAll(this.tempFileList);
                this.tempFileList.clear();
                getFilesAllName(this.indexPath);
                this.filePackageAdapter.notifyDataSetChanged();
                return;
            case R.id.iv_menu /* 2131230986 */:
                Log.e("iv_menu", "点击");
                initMenuPopupWindow(this.ivMenu);
                return;
            case R.id.key_search /* 2131231009 */:
                this.keywords = getStr(this.etquery);
                this.fileCenterList.clear();
                getKeyWordsLocalFile(Constant.SAVE_FILE_PACKAGE);
                return;
            case R.id.tv_close /* 2131231385 */:
                getActivity().findViewById(R.id.viewPager).setVisibility(8);
                getActivity().findViewById(R.id.rightTools_layout).setVisibility(0);
                return;
            case R.id.tv_localData /* 2131231423 */:
                this.recycleFile.setVisibility(0);
                this.recycleOnlineFile.setVisibility(8);
                this.tvLocalData.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
                this.tvLocalData.setTextColor(ContextCompat.getColor(getContext(), R.color.colorWhite));
                this.tvOnlineData.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorWhite));
                this.tvOnlineData.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
                this.etclear.setVisibility(8);
                getFilesAllName(Constant.SAVE_FILE_PACKAGE);
                return;
            case R.id.tv_onlineData /* 2131231431 */:
                this.recycleFile.setVisibility(8);
                this.recycleOnlineFile.setVisibility(0);
                this.tvLocalData.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorWhite));
                this.tvLocalData.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
                this.tvOnlineData.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
                this.tvOnlineData.setTextColor(ContextCompat.getColor(getContext(), R.color.colorWhite));
                if (this.appConfig.getMediaData() != null) {
                    ((FilePackagePresenter) this.presenter).getFileCenter(this.appConfig.getMediaData().getUrl().trim());
                    return;
                }
                return;
            default:
                return;
        }
    }

    public void initMenuPopupWindow(ImageView imageView) {
        if (this.menuPopupView == null) {
            View inflate = View.inflate(getActivity(), R.layout.popup_menu_file, null);
            this.menuPopupView = inflate;
            inflate.measure(0, 0);
        }
        if (this.menuPopupWindow == null) {
            this.menuPopupWindow = new PopupWindow(this.menuPopupView, getResources().getDimensionPixelOffset(R.dimen.dp_200), -2, true);
        }
        ((TextView) this.menuPopupView.findViewById(R.id.tv_import)).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.6
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                FilePackageFragment.this.showFileDialog();
                FilePackageFragment.this.menuPopupWindow.dismiss();
            }
        });
        ((TextView) this.menuPopupView.findViewById(R.id.tv_edit)).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.7
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                FilePackageFragment.this.menuPopupWindow.dismiss();
                Iterator it = FilePackageFragment.this.fileCenterList.iterator();
                while (it.hasNext()) {
                    ((FileCenter) it.next()).setDelete(true);
                }
                FilePackageFragment.this.filePackageAdapter.notifyDataSetChanged();
            }
        });
        this.menuPopupWindow.setOutsideTouchable(true);
        this.menuPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        Log.e("xxxxx", "" + getResources().getDimensionPixelOffset(R.dimen.dp_70));
        this.menuPopupWindow.showAtLocation(this.view, 53, getResources().getDimensionPixelOffset(R.dimen.dp_10), getResources().getDimensionPixelOffset(R.dimen.dp_120));
    }

    public void getFilesAllName(String str) {
        Log.e("path", str);
        File file = new File(str);
        file.exists();
        if (file.getParent().equals(new File(Constant.SAVE_FILE_PACKAGE).getParent())) {
            this.tvBack.setVisibility(8);
            this.tvClose.setVisibility(0);
        } else {
            this.tvBack.setVisibility(0);
            this.tvClose.setVisibility(8);
        }
        File[] listFiles = file.listFiles();
        if (listFiles == null) {
            Log.e("error", "空目录");
            return;
        }
        if (listFiles.length == 0 && this.fileCenterList.size() != 0) {
            this.parentPath = this.fileCenterList.get(0).getHerf();
        }
        this.fileCenterList.clear();
        for (int i = 0; i < listFiles.length; i++) {
            FileCenter fileCenter = new FileCenter();
            fileCenter.setHerf(listFiles[i].getAbsolutePath());
            fileCenter.setName(listFiles[i].getName());
            if (listFiles[i].isDirectory()) {
                fileCenter.setType(1);
            } else {
                fileCenter.setType(0);
            }
            this.fileCenterList.add(fileCenter);
        }
        this.filePackageAdapter.notifyDataSetChanged();
    }

    public void getKeyWordsLocalFile(String str) {
        File[] listFiles = new File(str).listFiles();
        if (listFiles == null) {
            L.d("本地公文包是空的");
        } else if (this.keywords.isEmpty()) {
            Toast.makeText(getActivity(), "搜索内容为空", 0).show();
        } else {
            for (int i = 0; i < listFiles.length; i++) {
                String name = listFiles[i].getName();
                if (listFiles[i].isDirectory()) {
                    getKeyWordsLocalFile(listFiles[i].getAbsolutePath());
                } else if (name.contains(this.keywords)) {
                    FileCenter fileCenter = new FileCenter();
                    fileCenter.setHerf(listFiles[i].getAbsolutePath());
                    fileCenter.setName(listFiles[i].getName());
                    fileCenter.setType(0);
                    this.fileCenterList.add(fileCenter);
                }
            }
            this.filePackageAdapter.notifyDataSetChanged();
        }
    }

    public void getAllLocalFile(String str) {
        File[] listFiles = new File(str).listFiles();
        if (listFiles == null) {
            return;
        }
        for (int i = 0; i < listFiles.length; i++) {
            if (listFiles[i].isDirectory()) {
                getAllLocalFile(listFiles[i].getAbsolutePath());
            } else {
                FileCenter fileCenter = new FileCenter();
                fileCenter.setHerf(listFiles[i].getAbsolutePath());
                fileCenter.setName(listFiles[i].getName());
                fileCenter.setType(0);
                this.localFileList.add(fileCenter);
            }
        }
    }

    @OnClick({R.id.tv_back})
    public void onViewClicked() {
        Log.e("tv_back", "" + this.indexPath);
        if (this.fileCenterList.size() > 0) {
            getFilesAllName(new File(new File(this.fileCenterList.get(0).getHerf()).getParent()).getParent());
        } else {
            getFilesAllName(new File(this.parentPath).getParent());
        }
    }

    public void showFileDialog() {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_import_file, (ViewGroup) null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(linearLayout).setCancelable(false);
        AlertDialog create = builder.create();
        this.fileDialog = create;
        create.show();
        ((TextView) linearLayout.findViewById(R.id.tv_finish)).setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.fragment.FilePackageFragment.8
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                FilePackageFragment.this.fileDialog.dismiss();
            }
        });
    }

    public static void deleteDirWihtFile(File file) {
        File[] listFiles;
        if (file != null && file.exists() && file.isDirectory()) {
            for (File file2 : file.listFiles()) {
                if (file2.isFile()) {
                    Log.e("file.isFile()", file2.getName());
                    file2.delete();
                } else if (file2.isDirectory()) {
                    deleteDirWihtFile(file2);
                }
            }
            file.delete();
        }
    }

    @Override // com.gisocn.onemap.view.FilePackageView
    public void getFileCenter(ArrayList<FileCenter> arrayList) {
        this.onlineFileList.clear();
        this.onlineFileList.addAll(arrayList);
        Iterator<FileCenter> it = this.onlineFileList.iterator();
        while (it.hasNext()) {
            FileCenter next = it.next();
            if (next.getName().equals("[To Parent Directory]")) {
                next.setName("返回上一级");
            }
        }
        this.handler.sendEmptyMessage(1);
    }
}
